let Common = require("../common/redisUtils");
const client = require("../common/redis");
const dbo = require("../common/database");

class Service extends Common {
  constructor(ctx, ctxs) {
    super();
    this.ctx = ctx;
    this.ctxs = ctxs;
  }

  async reset(query) {
    let userList;
    try {
      let userList = await dbo.findList("users");
      userList = userList.filter((item) => item.role == 2);
      userList = userList.map((item) => {
        return {
          ...item,
          isDaka: false,
          helping: false,
          uTime: undefined,
          updateTime: undefined,
        };
      });
    } catch (error) {
      this.ctx.send(this.handleError(query, error));
    }

    // 清空数据库
    client.ltrim("userList", -1, -2, (err, res) => {
      if (!err) {
        userList.forEach((item) => {
          client.lpush("userList", this.toJson(item), async (err, res) => {
            if (err) {
              this.ctx.send(this.handleError(query, err));
            } else {
              let res = await this.findAll("userList");
              this.ctx.send(this.handleSuccess(query, res));
            }
          });
        });
      } else {
        this.ctx.send(this.handleError(query, err));
      }
    });
  }

  async save(query) {
    client.lrange("userList", 0, -1, (err, res) => {
      let list = res.map((item) => {
        item = this.toObj(item); 
        return {
          // cTime: item.cTime,
          // check: item.check,
          // helping: item.helping,
          isDaka: item.isDaka,
          // password: item.password,
          role: item.role,
          uTime: item.uTime,
          username: item.username,
          // xrkId: item.xrkId,
          userId: item._id,
        };
      });
      dbo
        .insertMany("signs", list)
        .then((res) => {
          this.ctx.send(this.handleSuccess(query, res));
        })
        .catch((err) => {
          this.ctx.send(this.handleError(query, err));
        });
    });
  }

  async getUser(query) {
    try {
      let userList = await this.findAll("userList");
      let user = userList.find((item) => item.username === query.username);
      if (user) {
        this.ctx.send(this.handleSuccess(query, user));
      } else {
        this.ctx.send(this.handleError(query, { message: "没有这个用户" }));
      }
    } catch (error) {
      this.ctx.send(this.handleError(query, error));
    }
  }

  async getList(query) {
    client.lrange("userList", 0, -1, (err, res) => {
      if (err) {
        this.ctx.send(this.handleError(query));
      } else {
        this.ctx.send(this.handleSuccess(query, res));
      }
    });
  }

  async getHelp(query) {
    let userList = await this.findAll("userList");
    let index = this.getIndex(userList, "username", query.username);
    let newUser = this.toJson({
      ...userList[index],
      helping: query.helping,
      helpId: query.helpId,
      desc: query.desc,
    });
    client.lset("userList", index, newUser, (err, res) => {
      if (err) {
        this.ctx.send(this.handleError(query, err));
      } else {
        this.ctx.send(this.handleSuccess(query, newUser));
        this.ctxs[0].send(this.handleSuccess(query, newUser));
      }
    });
  }

  async sign(query) {
    let userList = await this.findAll("userList");
    let index = this.getIndex(userList, "username", query.username);
    let newUser = this.toJson({
      ...userList[index],
      isDaka: query.isDaka,
      uTime: Date.now(),
    });

    client.lset("userList", index, newUser, (err, res) => {
      if (err) {
        this.ctx.send(this.handleError(query, err));
      } else {
        this.ctx.send(this.handleSuccess(query, newUser));
        this.ctxs[0].send(this.handleSuccess(query, newUser));
      }
    });
  }

  async sendMsg(query) {
    let { msg } = query;
    this.ctxs.forEach((ctx) => {
      ctx.send(this.handleSuccess(query, { msg }));
    });
  }
}
module.exports = Service;
